package com.xd.pre.modules.tpshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xd.pre.modules.tpshop.domain.TpCart;
import com.xd.pre.modules.tpshop.mapper.TpCartMapper;
import com.xd.pre.modules.tpshop.service.ITpCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lx
 * @since 2020-06-03
 */
@Service
public class TpCartServiceImpl extends ServiceImpl<TpCartMapper, TpCart> implements ITpCartService {

    @Autowired
    TpCartMapper cartMapper;

    @Override
    public TpCart findCart_by_ugm(int userId, int goodsId, Integer specId) {
        LambdaQueryWrapper<TpCart> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TpCart::getUserId, userId);
        wrapper.eq(TpCart::getGoodsId, goodsId);
        wrapper.eq(TpCart::getMiddleSpecId, specId);
        TpCart one = getOne(wrapper);
        return one;
    }

    @Override
    public TpCart find_cart_byCartId(Integer cartId) {
        return getById(cartId);
    }

    @Override
    public int update_cart_number(int number, Integer cartId) {
        TpCart oo = new TpCart();
        oo.setGoodsNum(number);
        LambdaQueryWrapper<TpCart> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TpCart::getCartId, cartId);
        int update = cartMapper.update(oo, lambdaQuery);
        return update;
    }

    @Override
    public List<TpCart> findAll_cart() {
        return list();
    }

    @Override
    public int update_cart_by_postJsonArray(int goods_num, int select, int cartId) {
        TpCart oo = new TpCart();
        oo.setGoodsNum(goods_num);
        oo.setSelected(select);
        oo.setCartId(cartId);
        int update = cartMapper.updateById(oo);
        return update;
    }

    @Override
    public int deleteCart_afterSubmitOrder(int user_id) {
        LambdaQueryWrapper<TpCart> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TpCart::getUserId, user_id);
        lambdaQuery.eq(TpCart::getSelected, 1);
        int delete = cartMapper.delete(lambdaQuery);
        return delete;
    }

    @Override
    public List<TpCart> findCartList_withChecked() {
        LambdaQueryWrapper<TpCart> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TpCart::getSelected, 1);
        List<TpCart> list = list(lambdaQuery);
        return list;
    }

    @Override
    public int findCount_selectCartPO(int userId) {
        LambdaQueryWrapper<TpCart> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TpCart::getSelected, 1);
        lambdaQuery.eq(TpCart::getUserId, userId);
        return count(lambdaQuery);
    }
}
